/*
 *  Chromosome.cpp
 *  Genetski
 *
 *  Created by Srđan Rašić on 11/19/11.
 *
 */

#include "Chromosome.h"
#include <cstdlib>
#include <cmath>
#include <iostream>

Chromosome::Chromosome(int parts, int partSize) : std::string(parts*partSize, '0') {
	_parts = parts;
	_partSize = partSize;

	for (int i = 0; i < size(); i++) {
		double r = rand() / (double)RAND_MAX;
		if (r > 0.5)
			(*this)[i] = '1';
	}
}

int Chromosome::PartLength() const {
	return _partSize;
}

double Chromosome::Value(int part) {
	double r = 0.0;
	
	for (int i = part * _partSize + _partSize - 1; i >= part * _partSize; i--)
		if ((*this)[i] == '1')
			r += pow(2, part * _partSize + _partSize - 1 - i);
	
	return r;
}

std::vector<double> Chromosome::Values() {
	std::vector<double> r(_parts);
	
	for (int i = 0; i < _parts; i++)
		r[i] = Value(i);
	
	return r;
}